package com.health.yb.web.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.health.yb.core.exception.CommonException;
import com.health.yb.web.bean.common.NameReq;
import com.health.yb.web.bean.common.Response;
import com.health.yb.web.bean.entity.YbType;
import com.health.yb.web.service.YbTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author rjwang
 * @version 1.0
 * @date 2019/12/15 16:08
 */
@Api(tags = {"类型"})
@RestController
@Validated
public class TypeController {

    @Autowired
    private YbTypeService ybTypeService;

    @PostMapping("/type")
    @ApiOperation("新增类型")
    public Response addType(@Validated @RequestBody NameReq nameReq) throws Exception {
        String typeName = nameReq.getName();
        if (StrUtil.isBlank(typeName)) {
            return new Response().failureWithMsg("物资名称必须传");
        }
        Integer count = ybTypeService.count(new LambdaQueryWrapper<YbType>().eq(YbType::getTypeName, typeName));
        if (count > 1) {
            return new Response().failureWithMsg("类型存在，请重新填写");
        }
        return ybTypeService.addType(typeName);
    }

    @GetMapping("/type")
    @ApiOperation("查询类型")
    public Response<YbType> getTypes() throws Exception {
        return new Response().successWithData(ybTypeService.list(new LambdaQueryWrapper<YbType>()));
    }


}
